<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

<head>
    <th:block th:include="include :: header('联盟推广列表')" />
    <th:block th:include="include :: select2-css" />
    <th:block th:include="include :: bootstrap-select-css" />
</head>

<body class="gray-bg">
    <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <P>商品名字：</P>
                                <input class="form-control" type="text" name="shopName" />
                            </li>
                            <li>
                                <P> 店铺名字：</P>
                                <input class="form-control" type="text" name="homeName" />
                            </li>
                            <li>
                                <P> 商品状态：</P> <select class="form-control" name="visible"
                                    th:with="type=${@dictTag.getType('cms_union_visible')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <P> 关联标签：</P>
                                <input class="form-control" type="text" name="tags" />
                            </li>
                            <li>
                                <P> 关联分类：</P>
                                <input class="form-control" type="text" name="cats" />
                            </li>
                            <li>
                                <P>选择平台:</P>
                                <select name="unionType" class="form-control"
                                    th:with="types=${@dictTag.getType('cms_union_type')}">
                                    <option value="">
                                        不限
                                    </option>
                                    <option th:each="dict : ${types}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}">
                                    </option>
                                </select>
                            </li>
                            <li class="select-time">
                                <p>创建时间： </p>
                                <input type="text" class="time-input" id="startTime" placeholder="开始时间"
                                    name="params[beginTime]" />
                                <span>-</span>
                                <input type="text" class="time-input" id="endTime" placeholder="结束时间"
                                    name="params[endTime]" />
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                        class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                        class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <div class="btn-group" shiro:hasPermission="cms:union:edit">
                    <button data-toggle="dropdown" class="btn btn-primary dropdown-toggle">状态设置 <span
                            class="caret"></span>
                    </button>
                    <ul class="dropdown-menu" id="visible" th:with="type=${@dictTag.getType('cms_union_visible')}">
                        <li th:each="dict : ${type}" th:data-id="${dict.dictValue}"><a href="javascript:void(0)"
                                th:text="${dict.dictLabel}"></a></li>
                    </ul>
                </div>
                <div class="btn-group" shiro:hasPermission="cms:union:edit">
                    <button data-toggle="dropdown" class="btn btn-primary dropdown-toggle multiple disabled">关联标签<span
                            class="caret"></span>
                    </button>
                    <ul class="dropdown-menu" id="conTag">
                        <li th:each="tag : ${tags}" th:data-id="${tag.tagId}"><a href="javascript:void(0)"
                                th:text="${tag.tagName}"></a></li>
                    </ul>
                </div>
                <a class="btn btn-success multiple disabled" shiro:hasPermission="cms:union:edit" onclick="editCat()"> 关联分类
                </a>
                <a class="btn btn-success" onclick="$.operate.addTab()" shiro:hasPermission="cms:union:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.editTab()"
                    shiro:hasPermission="cms:union:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"
                    shiro:hasPermission="cms:union:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="cms:union:export">
                    <i class="fa fa-download"></i> 导出
                </a>
                <a class="btn btn-info" onclick="$.importUnionExcel()" shiro:hasPermission="cms:union:import">
                    <i class="fa fa-upload"></i> 导入
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: select2-js" />
    <th:block th:include="include :: bootstrap-select-js" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('cms:union:edit') }]];
        var removeFlag = [[${@permission.hasPermi('cms:union:remove') }]];
        var fi = [[${@dictTag.getType('cms_union_visible') }]];
        var ut = [[${@dictTag.getType('cms_union_type') }]];
        let catMaxNum = [[${@configTag.getKey('cmsConfig', 'oly.cms.union.catSize') }]];
        let tagMaxNum = [[${@configTag.getKey('cmsConfig', 'oly.cms.union.tagSize') }]];
        var prefix = ctx + "cms/union";
        $(function () {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                importUrl: prefix + "/importData",
                importTemplateUrl: prefix + "/importTemplate",
                sortName: "createTime",
                sortOrder: "desc",
                modalName: "淘客",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'shopId',
                    title: '商品id',
                    visible: false
                },
                {
                    field: 'shopName',
                    title: '商品名字',
                    editable: {
                        type: 'text',
                        title: '商品名字',
                        emptytext: "商品为空",
                        validate: function (value) {
                            if (value.length > 60) {
                                return '名称不能超过30个字符';
                            }
                            if (value.length == 0) {
                                return '名称不能为空';
                            }
                        }
                    },
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'visible',
                    title: '商品状态',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(fi, value);
                    }
                },
                {
                    field: 'unionType',
                    title: '来源',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(ut, value);
                    }
                },
                {
                    field: 'shopImg',
                    title: '商品主图',
                    formatter: function (value, row, index) {
                        return $.table.imageView(value);
                    }

                },
                {
                    field: 'shopIndex',
                    title: '商品主页',
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'homeName',
                    title: '店铺名字',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'shopPrice',
                    title: '商品价格',
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'shopSales',
                    title: '商品月销量',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'incomeRatio',
                    title: '收入比例',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'commission',
                    title: '佣金',
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'tags',
                    title: '标签列表'
                },
                {
                    field: 'cats',
                    title: '分类列表'
                },
                {
                    field: 'wangName',
                    title: '商家旺旺名',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'sortUrl',
                    title: '淘宝客短链接',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'taokeUrl',
                    title: '推广链接',
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'couponCount',
                    title: '优惠卷总量',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'couponNum',
                    title: '优惠卷剩余量',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'couponPrice',
                    title: '优惠卷价值',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'couponStartTime',
                    title: '优惠卷开始时间',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'couponEndTime',
                    title: '优惠卷结束时间',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'couponUrl',
                    title: '优惠卷领取地址',
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'couponCode',
                    title: '优惠券淘口',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'couponSortUrl',
                    title: '优惠卷领取短链',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'marketIs',
                    title: '是否为营销',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'groupNum',
                    title: '拼团人数',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'groupPrice',
                    title: '拼团价',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'groupCommission',
                    title: '团队佣金比例',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'groupStartTime',
                    title: '拼团开始时间',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'groupEndTime',
                    title: '拼团结束时间',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'remark',
                    title: '备注',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'shopContent',
                    title: '内容描述',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.shopId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });
        /*淘客下拉框点击事件*/
        $('#visible>li').click(function (e) {
            //获取选中行数据
            var pam = {}, ids = [], chooseData = $("#bootstrap-table").bootstrapTable('getSelections');
            pam.visible = $(this).attr("data-id");
            if (chooseData.length > 0) {
                $.each(chooseData, function (index, ele) {
                    ids.push(ele.shopId);
                });
                pam.ids = ids.join();
                $.modal.confirm("确认要批量修改联盟状态吗？", function () {
                    $.operate.post(prefix + "/updateVisible", pam);
                })
            }
            else {
                $.modal.alert("请至少选择一项");
            }
        });

        /*淘客下拉框点击事件*/
        $('#conTag>li').click(function (e) {
            //获取选中行数据
            let cmsUnions = [], chooseData = $("#bootstrap-table").bootstrapTable('getSelections'), chooseTagId = $(this).attr("data-id");
            if (chooseData.length > 0) {
                chooseData.forEach(e => {
                    let pum = {};
                    pum.shopId = e.shopId;
                    pum.tags = setTags(e.tags, chooseTagId, tagMaxNum);
                    cmsUnions.push(pum);
                });
                $.modal.confirm("确认要修改联盟关联标签吗?数量超出将自动忽略!重复将自动删除", function () {
                    $.ajax({
                        type: "post",
                        url: prefix + "/updateTags",
                        contentType: 'application/json; charset=UTF-8',
                        data: JSON.stringify(cmsUnions),
                        dataType: "json",
                        async: false,
                        success: function (data) {
                            $.operate.ajaxSuccess(data);
                        }
                    });
                });
            }
            else {
                $.modal.alert("请选择一项");
            }
        });

        function editCat() {    
            var url = ctx + "cms/cat/selectCatTree?catId="+[[${@configTag.getKey('cmsConfig', 'oly.cms.union.catId')}]]+"&visible=1&catType=" + [[${@configTag.getKey('cmsConfig', 'oly.cms.union.type')}]];
        var optionsCat = {
            title: '分类选择',
            width: "380",
            url: url,
            callBack: function (index, layero) {
                //获取选中行数据
                let cmsUnions = [], chooseData = $("#bootstrap-table").bootstrapTable('getSelections'),catId= $.modal.getChildFrame(index).find('#treeId').val();
                if (chooseData.length > 0) {
                    chooseData.forEach(e => {
                        let pum = {};
                        pum.shopId = e.shopId;
                        pum.cats = setTags(e.cats, catId, catMaxNum);
                        cmsUnions.push(pum);
                    });
                    $.modal.confirm("确认要修改联盟关联分类吗?数量超出将自动忽略!重复将自动删除", function () {
                        $.ajax({
                            type: "post",
                            url: prefix + "/updateCats",
                            contentType: 'application/json; charset=UTF-8',
                            data: JSON.stringify(cmsUnions),
                            dataType: "json",
                            async: false,
                            success: function (data) {
                                $.operate.ajaxSuccess(data);
                                $.modal.close(index);
                            }
                        });
                    });
                }
                else {
                    $.modal.alert("请选择一项");
                }
            }
        };
        $.modal.openOptions(optionsCat);
        }

        (function ($) {
            $.extend({
                // 导入淘客数据
                importUnionExcel: function (formId) {
                    table.set();
                    var currentId = $.common.isEmpty(formId) ? 'importTpl' : formId;
                    layer.open({
                        type: 1,
                        fix: false,
                        //不固定
                        maxmin: true,
                        offset: '30px',
                        shade: 0.3,
                        title: '导入' + table.options.modalName + '数据',
                        content: $('#' + currentId).html(),
                        success: function (index, layero) {
                            $("#selectType").selectpicker("refresh");
                            $("#tags").selectpicker("refresh");
                        },
                        btn: ['<i class="fa fa-check"></i> 导入', '<i class="fa fa-remove"></i> 取消'],
                        // 弹层外区域关闭
                        shadeClose: true,
                        btn1: function (index, layero) {
                            var file = layero.find('#file').val();
                            if (file == '' || (!$.common.endWith(file, '.xls') && !$.common.endWith(file, '.xlsx'))) {
                                $.modal.msgWarning("请选择后缀为 “xls”或“xlsx”的文件。");
                                return false;
                            }
                            var index = layer.load(2, {
                                shade: false
                            });
                            $.modal.disable();
                            var formData = layero.find('#' + currentId + 'Form').serializeArray();
                            //获取表单数据
                            formData.push({
                                "name": "file",
                                "value": layero.find('#file')[0].files[0]
                            });
                            var formData1 = new FormData();
                            for (var p in formData) {
                                formData1.append(formData[p].name, formData[p].value);
                            }
                            $.ajax({
                                url: table.options.importUrl,
                                data: formData1,
                                cache: false,
                                contentType: false,
                                processData: false,
                                type: 'POST',
                                success: function (result) {
                                    if (result.code == web_status.SUCCESS) {
                                        $.modal.closeAll();
                                        layer.closeAll();
                                        $.modal.alertSuccess(result.msg);
                                        $.table.refresh();
                                    } else if (result.code == web_status.WARNING) {
                                        layer.close(index);
                                        $.modal.enable();
                                        $.modal.alertWarning(result.msg)
                                    } else {
                                        layer.close(index);
                                        $.modal.enable();
                                        $.modal.alertError(result.msg);
                                    }
                                }
                            });
                        }
                    });
                },
            })
        })(jQuery);


        /*栏目管理-新增-选择菜单树*/
        $(document).on('click', '#cats', function () {
            let ui = $(this);
            var url = ctx + "cms/cat/selectCatTree?catId=0&visible=1&catType=" + [[${@configTag.getKey('cmsConfig', 'oly.cms.union.type')}]];
        var options = {
            title: '分类选择',
            width: "380",
            url: url,
            callBack: function (index, layero) {
                var body = $.modal.getChildFrame(index);
                let oldValue = $(ui).val();
                let cc = body.find('#treeId').val();
                $(ui).val(setTags(oldValue, cc, catMaxNum));
                $.modal.close(index);
            }
        };
        $.modal.openOptions(options);
        });

        function setTags(oldValue, cc, maxNum) {
            let oldArray = [];
            if (oldValue == '' || oldValue == null) {
                oldValue = cc;
            }
            else {
                oldArray = oldValue.split(',');
                if (oldArray.indexOf(cc) == -1) {
                    if (oldArray.length > maxNum - 1) {
                        console.log("超出允许关联数量:" + maxNum);
                    }
                    else {
                        oldValue = oldValue + ',' + cc;
                    }
                }
                else {
                    oldArray = oldArray.filter(item => item != cc);

                    oldValue = oldArray.join(',');
                }
            }
            return oldValue;
        }
    </script>
    <!-- 导入区域 -->
    <script id="importTpl" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb10" id="importTplForm">
        <div  style="padding:10px">
		<input type="file" id="file" name="file"/>
        <div class="mt10 pt5">
            <label class="font-noraml">选择平台</label>
              <select class="form-control noselect2 selectpicker" data-max-options="1" id="selectType"  data-live-search="true" name="unionType" th:with="type=${@dictTag.getType('cms_union_type')}">
                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}">
                 </option>
             </select>
        </div>
        
        <div class="mt10 pt5">
            <label class="font-noraml">关联分类</label>
            <input class="form-control" type="input" name="cats" id="cats" readonly>
        </div>
       
        <div class="mt10 pt5">
            <label class="font-noraml">关联标签</label>
            <select class="form-control select2-multiple" multiple th:data-max-options="${@configTag.getKey('cmsConfig','oly.cms.union.tagSize')}" data-none-selected-text="请选择"  data-live-search="true"  id="tags" name="tags">
                <option th:each="tag : ${tags}" th:text="${tag.tagName}" th:value="${tag.tagId}">
                 </option>
             </select>
        </div>

		<div class="mt10 pt5">
			<input type="checkbox" id="updateSupport" name="updateSupport" title="如果数据已经存在，更新这条数据。"> 是否更新已经存在的联盟数据
			 &nbsp;	<a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> 下载模板</a>
		</div>
		<font color="red" class="pull-left mt10">
			提示：仅允许导入“xls”或“xlsx”格式文件！
		</font>
	    </div>          
    </form>
</script>

</body>

</html>